<template>
	<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk"
	 @cancel="handleCancel" cancelText="关闭">
		<a-spin :spinning="confirmLoading">
			<a-form :form="form">
				<!-- <a-form-item label="行号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['rowNum']" placeholder="请输入行号"></a-input>
        </a-form-item> -->
				<!-- <a-form-item label="料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup
            v-decorator="['materielCode']"
            :trigger-change="true"
            org-fields="id,materiel_code,product_name,unit"
            dest-fields="materielId,materielCode,materielName,orderUnit"
            code="materiel_search"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="物料" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielName']" placeholder="请输入物料"></a-input>
        </a-form-item> -->
				<a-form-item label="料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-popup v-decorator="['materielCode',validatorRules.materielCode]" :trigger-change="true" org-fields="id,materiel_code,product_name,product_code,gauge,unit,query2"
					 dest-fields="materielId,materielCode,materielName,productCode,materielGauge,orderUnit,clientCode" code="materiel_search" @callback="popupCallback" />
				</a-form-item>
				<a-form-item label="品名" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['materielName']" placeholder="请输入品名" disabled></a-input>
				</a-form-item>
				<a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['materielGauge']" placeholder="请输入规格" disabled></a-input>
				</a-form-item>
        <a-form-item label="客户料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['clientCode']" placeholder="请输入客户料号" disabled></a-input>
        </a-form-item>
        <a-form-item label="商品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['productCode']" placeholder="请输入商品编码" disabled></a-input>
        </a-form-item>
				<!-- <a-form-item label="短文本" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['shortText']" placeholder="请输入短文本"></a-input>
				</a-form-item> -->
				<a-form-item label="采购数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['purchaseNum',validatorRules.purchaseNum]" placeholder="请输入采购数量"></a-input>
				</a-form-item>
        <a-form-item label="未收货数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['unreceiveNum',validatorRules.unreceiveNum]" placeholder="请输入未收货数量"></a-input>
        </a-form-item>
				<a-form-item label="订单单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['orderUnit']" placeholder="请输入订单单位"></a-input>
				</a-form-item>
        <a-form-item label="最小包装数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['query4',validatorRules.query4]" :trigger-change="true" placeholder="请输入最小包装数"></a-input>
        </a-form-item>
				<a-form-item label="净价" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['grossPrice']" placeholder="请输入净价"></a-input>
				</a-form-item>
				<a-form-item label="货币" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-dict-select-tag type="list" v-decorator="['currency']" :trigger-change="true" dictCode="current_money"
					 placeholder="请选择货币" />
				</a-form-item>
				<a-form-item label="交货日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-date placeholder="请选择交货日期" v-decorator="['deliveryDate']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"
					 style="width: 100%" />
				</a-form-item>
				<!-- <a-form-item label="物料组" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-search-select-tag v-decorator="['materielTeam']" dict="mes_chiefdata_materielgroup,group_name,group_name" />
				</a-form-item> -->
				<!-- <a-form-item label="工厂" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup
            v-decorator="['factoryName']"
            :trigger-change="true"
            org-fields="id,fac_nameone,store_name"
            dest-fields="factoryId,factoryName,storageSite"
            code="factorysite_search"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="库存地点" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['storageSite']" placeholder="请输入库存地点"></a-input>
        </a-form-item> -->
				<a-form-item label="工厂编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-popup v-decorator="['factoryCode',validatorRules.factoryCode]" :trigger-change="true" org-fields="id,fac_code,fac_nameone,store_name"
					 dest-fields="factoryId,factoryCode,factoryName,storageSite" code="factorysite_search" @callback="popupCallback" />
				</a-form-item>
				<a-form-item label="工厂名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['factoryName']" placeholder="请输入工厂名称"disabled></a-input>
				</a-form-item>
				<a-form-item label="库存地点" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['storageSite']" placeholder="请输入库存地点"disabled></a-input>
				</a-form-item>
        <a-form-item label="物料标记" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<a-input v-decorator="['imgExgFlag']" placeholder="请输入物料标记"></a-input>
        </a-form-item>
       <!-- <a-form-item label="项号" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<a-input v-decorator="['itemNo']" placeholder="请输入项号"></a-input>
        </a-form-item>
        <a-form-item label="批次号" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<a-input v-decorator="['sectionNo']" placeholder="请输入批次号"></a-input>
        </a-form-item>
				<a-form-item label="公司编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['companyCode']" placeholder="请输入公司编码"></a-input>
				</a-form-item> -->
        <a-form-item label="板数" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<a-input v-decorator="['palletNum']" placeholder="请输入板数"></a-input>
        </a-form-item>
        <a-form-item label="件数" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<a-input v-decorator="['pcs']" placeholder="请输入件数"></a-input>
        </a-form-item>
        <a-form-item label="包装种类" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<a-input v-decorator="['wrap']" placeholder="请输入包装种类"></a-input>
        </a-form-item>
        <a-form-item label="箱号" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<a-input v-decorator="['containerNo']" placeholder="请输入箱号"></a-input>
        </a-form-item>
        <a-form-item label="净重" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<a-input v-decorator="['netWeight']" placeholder="请输入净重"></a-input>
        </a-form-item>
        <a-form-item label="毛重" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<a-input v-decorator="['grossWeight']" placeholder="请输入毛重"></a-input>
        </a-form-item>
				<a-form-item label="需求编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['requireCode']" placeholder="请输入需求编号"></a-input>
				</a-form-item>
				<a-form-item label="申请者" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['applicant']" placeholder="请输入申请者"></a-input>
				</a-form-item>
				<a-form-item v-show="false" label="物料ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-show="false" v-decorator="['materielId']" placeholder="请输入物料ID"></a-input>
				</a-form-item>
				<a-form-item v-show="false" label="工厂ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-show="false" v-decorator="['factoryId']" placeholder="请输入工厂ID"></a-input>
				</a-form-item>

			</a-form>
		</a-spin>
	</j-modal>
</template>

<script>
	import {
		httpAction
	} from '@/api/manage'
	import pick from 'lodash.pick'
	import {
		validateDuplicateValue
	} from '@/utils/util'
	import JDate from '@/components/jeecg/JDate'
	import JDictSelectTag from "@/components/dict/JDictSelectTag"
	import JSearchSelectTag from '@/components/dict/JSearchSelectTag'

	export default {
		name: "MesPurchaseItemModal",
		components: {
			JDate,
			JDictSelectTag,
			JSearchSelectTag,
		},
		props: {
			mainId: {
				type: String,
				required: false,
				default: ''
			}
		},
		data() {
			return {
				form: this.$form.createForm(this),
				title: "操作",
				width: 800,
				visible: false,
				model: {},
				labelCol: {
					xs: {
						span: 24
					},
					sm: {
						span: 5
					},
				},
				wrapperCol: {
					xs: {
						span: 24
					},
					sm: {
						span: 16
					},
				},

				confirmLoading: false,
				validatorRules: {
					factoryCode: {
						rules: [{
							required: true,
							message: '请选择工厂编号!'
						}, ]
					},
          materielCode: {
          	rules: [{
          		required: true,
          		message: '物料料号不能为空!'
          	}, ]
          },
          purchaseNum: {
          	rules: [{
          		required: true,
          		message: '采购数量不能为空!'
          	}, ]
          },
          unreceiveNum: {
            rules: [{
              required: true,
              message: '未收货数量不能为空!'
            }, ]
          },
          query4: {
            rules: [{
              required: true,
              message: '批次数量不能为空!'
            }, ]
          },
				},
				url: {
					add: "/order/mesOrderPurchase/addMesPurchaseItem",
					edit: "/order/mesOrderPurchase/editMesPurchaseItem",
				}

			}
		},
		created() {},
		methods: {
			add() {
				this.edit({});
			},
			edit(record) {
				this.form.resetFields();
				this.model = Object.assign({}, record);
				this.visible = true;
				this.$nextTick(() => {
					this.form.setFieldsValue(pick(this.model, 'createBy', 'createTime', 'updateBy', 'updateTime', 'sysOrgCode',
						'materielId', 'materielCode', 'materielName', 'materielGauge', 'shortText', 'purchaseNum', 'orderUnit',
						'grossPrice', 'currency', 'deliveryDate', 'materielTeam', 'factoryId', 'factoryCode', 'factoryName',
						'storageSite', 'batchNum', 'requireCode', 'applicant', 'purchaseId', 'rowNum', 'unreceiveNum', 'query3',
						'query4', 'query5','imgExgFlag','itemNo','sectionNo','companyCode','palletNum','pcs','wrap','containerNo','netWeight','grossWeight','query6','productCode','clientCode'))
				})
			},
			close() {
				this.$emit('close');
				this.visible = false;
			},
			handleOk() {
				const that = this;
				// 触发表单验证
				this.form.validateFields((err, values) => {
					if (!err) {
						that.confirmLoading = true;
						let httpurl = '';
						let method = '';
						if (!this.model.id) {
							httpurl += this.url.add;
							method = 'post';
						} else {
							httpurl += this.url.edit;
							method = 'put';
						}
						let formData = Object.assign(this.model, values);
						formData['purchaseId'] = this.mainId
						console.log("表单提交数据", formData)
						httpAction(httpurl, formData, method).then((res) => {
							if (res.success) {
								that.$message.success(res.message);
								that.$emit('ok');
							} else {
								that.$message.warning(res.message);
							}
						}).finally(() => {
							that.confirmLoading = false;
							that.close();
						})
					}

				})
			},
			handleCancel() {
				this.close()
			},
			popupCallback(row) {
				this.form.setFieldsValue(pick(row, 'createBy', 'createTime', 'updateBy', 'updateTime', 'sysOrgCode', 'materielId',
					'materielCode', 'materielName', 'materielGauge', 'shortText', 'purchaseNum', 'orderUnit', 'grossPrice',
					'currency', 'deliveryDate', 'materielTeam', 'factoryId', 'factoryCode', 'factoryName', 'storageSite', 'batchNum',
					'requireCode', 'applicant', 'purchaseId', 'rowNum', 'unreceiveNum','imgExgFlag','itemNo','sectionNo','companyCode','palletNum','pcs','wrap','containerNo','netWeight','grossWeight','query3', 'query4', 'query5', 'query6','productCode','clientCode'))
			},


		}
	}
</script>
